package com.code2roc.jarmanage.bizloigc;

import com.code2roc.jarmanage.db.CommandDB;
import com.code2roc.jarmanage.db.ProjectDB;
import com.code2roc.jarmanage.model.CommandDO;
import com.code2roc.jarmanage.model.FileInfo;
import com.code2roc.jarmanage.model.ProjectDO;
import com.code2roc.jarmanage.util.ConfigUtil;
import com.code2roc.jarmanage.util.EncryptUtil;
import com.code2roc.jarmanage.util.FileUtil;
import com.code2roc.jarmanage.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ProjectBizloigc {

    public static List<FileInfo> getConfigFileList(String projectID){
        ProjectDO entity = ProjectDB.selectOne(projectID);
        List<FileInfo> configFileInfoList = new ArrayList<>();
        List<String> fileExtList = Arrays.asList(ConfigUtil.getConfigValue("configFileExt").split("\\,"));
        String filePath = "";
        if (StringUtil.isEmpty(entity.getDepolyType())) {
            filePath = entity.getJarPackagePath();
        } else {
            filePath = entity.getJarPackagePath() + File.separator + "webapps";
        }
        File file = new File(filePath);
        File[] fileList = file.listFiles();
        for (File f : fileList) {
            if (f.isFile()) {
                boolean flag = false;
                for (String fileExt : fileExtList) {
                    if (f.getAbsolutePath().endsWith(fileExt)) {
                        flag = true;
                        break;
                    }
                }
                if (flag && !f.getName().startsWith(entity.getServiceName())) {
                    FileInfo configFileInfo = new FileInfo();
                    configFileInfo.setFileName(f.getName());
                    try {
                        configFileInfo.setFileID(EncryptUtil.encryptByMD5(f.getName()));
                    } catch (Exception e) {

                    }
                    configFileInfo.setCreateDate(FileUtil.getCreationTime(f));
                    configFileInfo.setLastModifyDate(FileUtil.getLastModifyTime(f));
                    configFileInfoList.add(configFileInfo);
                }
            }
        }
        return configFileInfoList;
    }

    public static List<CommandDO> getCommondList(String projectID){
        return CommandDB.selectList("projectID = '"+projectID+"'");
    }
}
